package com.shipwe.loans.ui.activity

import android.view.View
import com.shipwe.loans.R
import com.shipwe.loans.databinding.ActivityAuthStepsBinding
import com.shipwe.loans.databinding.ActivityAuthStepsItemTitleBinding
import com.shipwe.loans.pro.ShipweActivity
import com.shipwe.loans.ui.fragment.FragmentStepAuthCommon
import com.shipwe.loans.ui.fragment.FragmentStepAuthContact
import com.shipwe.loans.ui.fragment.FragmentStepAuthFace
import com.shipwe.loans.ui.fragment.FragmentStepAuthOcr
import com.shipwe.loans.ui.fragment.FragmentStepAuthWallet
import com.shipwe.loans.ui.presenter.AuthStepsPresenter
import com.shipwe.loans.ui.view.IAuthStepsView
import com.shipwe.loans.utils.AuthStepsPickContactUtils
import com.shipwe.loans.utils.event.AppEventData
import com.shipwe.loans.utils.event.AppEventPointType
import org.json.JSONObject

/**
 * Shipwe
 * @Author wwdeng
 * @CreateTime: 2024/11/18 18:51
 * @Annotation：进件流程
 */
open class ActivityAuthSteps: ShipweActivity(), IAuthStepsView {
    lateinit var binding: ActivityAuthStepsBinding

    var presenter: AuthStepsPresenter? = null
    //选择联系人
    var pickContact: AuthStepsPickContactUtils? = null
    override fun getShipweRootView(): View? {
        binding = ActivityAuthStepsBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun initShipweView() {
        rootViewBinding.rootLayout.setBackgroundResource(R.drawable.page_login_bg)
        setShipweTitleBarVisibility(View.VISIBLE)
        setShipweBackBtnRes(R.drawable.back_black)
        setShipweRightBtnRes(R.drawable.customer_black)
        setShipweTextTitle(getString(R.string.steps_title))

        pickContact = AuthStepsPickContactUtils(this)
    }

    override fun initShipweListener() {
        rootViewBinding.rootCustomer.setOnClickListener {
            val eventTitle = presenter!!.getAuthStepEventTitle()
            showCustomerDialog(eventTitle)
            appEventPointRequestAction(AppEventPointType.CLICK.SHIPWE_CLICK_CUSTOMER,eventTitle)
        }

        //流程认证完成
        binding.stepsComplete.setOnClickListener {
            appEventPointRequestAction(AppEventPointType.CLICK.SHIPWE_CLICK_STEP_COMPLETE_CONTINUER)
            onIntent(ActivityAuthStepsCompleteWait::class.java)
            finish()
        }
    }

    override fun initShipweData() {
        presenter = AuthStepsPresenter(this)
        presenter!!.loadAuthStepPageAction()
    }

    /**
     * 所有步骤已完成
     */
    override fun showAuthStepsCompleteViews() {
        setShipweBackBtnVisibility(View.GONE)
        binding.stepsComplete.visibility = View.VISIBLE
    }

    /**
     * 移除所有步骤
     */
    override fun removeAuthStepsAllViews() {
        binding.afterSteps.removeAllViews()
        binding.afterSteps.visibility = View.GONE
        binding.curtStep.removeAllViews()
        binding.beforeSteps.removeAllViews()
        binding.beforeSteps.visibility = View.GONE
        binding.stepsComplete.visibility = View.INVISIBLE
    }

    /**
     * 已完成的步骤
     */
    override fun showAuthStepAfterView(stepTitle: String) {
        val itemView = ActivityAuthStepsItemTitleBinding.inflate(layoutInflater)
        itemView.authStepTitle.text = stepTitle
        itemView.authStepStatus.setImageResource(R.drawable.page_auth_step_complete)

        binding.afterSteps.addView(itemView.root)
        binding.afterSteps.visibility = View.VISIBLE
    }

    /**
     * 未开始的步骤
     */
    override fun showAuthStepBeforeView(stepTitle: String) {
        val itemView = ActivityAuthStepsItemTitleBinding.inflate(layoutInflater)
        itemView.authStepTitle.text = stepTitle
        itemView.authStepStatus.setImageResource(R.drawable.page_auth_step_before)

        binding.beforeSteps.addView(itemView.root)
        binding.beforeSteps.visibility = View.VISIBLE
    }

    /**
     * 联系人
     */
    override fun showAuthStepContactView(stepData: JSONObject) {
        replaceContainerFragement(R.id.curtStep, FragmentStepAuthContact(this,stepData))
    }

    /**
     * 证件照
     */
    override fun showAuthStepOcrView(stepData: JSONObject) {
        replaceContainerFragement(R.id.curtStep,FragmentStepAuthOcr(stepData))
    }

    /**
     * 人脸照
     */
    override fun showAuthStepSelfieView(stepData: JSONObject) {
        replaceContainerFragement(R.id.curtStep,FragmentStepAuthFace(stepData))
    }

    /**
     * 绑卡
     */
    override fun showAuthStepAccountView(stepData: JSONObject) {
        replaceContainerFragement(R.id.curtStep, FragmentStepAuthWallet(stepData))
    }

    /**
     * 通用样式
     */
    override fun showAuthStepCommonView(stepData: JSONObject) {
        replaceContainerFragement(R.id.curtStep, FragmentStepAuthCommon(stepData))
    }

    /**
     * 提交
     */
    open fun onAuthStepsSubmitAction(params: JSONObject){
        presenter!!.submitAuthStepsProcessAction(params)
    }

    /**
     * 流程提交成功
     * 需要发送通知刷新总流程
     */
    override fun showAuthStepsSubmitSuccess() {
        //通知刷新总流程
        publishEventData(AppEventData(AppEventData.TYPE.SHIPWE_PROCESS_REFRESH))
    }

    override fun onShipwePublishEvent(event: AppEventData) {
        super.onShipwePublishEvent(event)
        when(event.eventType){
            AppEventData.TYPE.SHIPWE_PROCESS_REFRESHED -> {
                presenter!!.loadAuthStepPageAction()
            }
            else -> {

            }
        }
    }

    override fun onStop() {
        super.onStop()
        binding.root.clearFocus()
    }
}